package com.piz.dao.order;

import java.util.List;

import com.piz.dao.common.PizHiberImpl;
import com.piz.dao.common.UnexpectedDBException;
import com.piz.model.BPO_Details;
import com.piz.model.BankDetails;
import com.piz.model.OfferCoupon;
import com.piz.model.Ordertxn;
import com.piz.model.Payment_Type;

/**
 * @author prameetk.sharma
 *
 */
@SuppressWarnings("unchecked")
public class OrderDao extends PizHiberImpl{
	
	/**
	 * @param bankDetails
	 * @return
	 */
	public Long addBankDetails(BankDetails bankDetails)
	{
		try {
			saveOrUpdate(bankDetails);
			return bankDetails.getId();
		} catch (Exception e) {
			throw new UnexpectedDBException("Error in saving bank  details.", e);
		}
	}
	
	/**
	 * @param id
	 * @return
	 */
	public BankDetails findBankById(Long id)
	{
		try {
			return (BankDetails) search(BankDetails.class, id);
		} catch (Exception e) {
			throw new UnexpectedDBException("Not able to find bank with id "+id, e);
		}
	}
	
	/**
	 * @param bpo
	 * @return
	 */
	public Long addBPOCenter(BPO_Details bpo)
	{
		try {
			saveOrUpdate(bpo);
			return bpo.getId();
		} catch (Exception e) {
			throw new UnexpectedDBException("Error in saving BPO center details", e);
		}
	}
	
	/**
	 * @param id
	 * @return
	 */
	public BPO_Details findBPOById(Long id)
	{
		try {
			return (BPO_Details) search(BPO_Details.class, id);
		} catch (Exception e) {
			throw new UnexpectedDBException("Not able to find bpo with id "+id, e);
		}
	}
	
	/**
	 * @param order_txn
	 * @return
	 */
	public Long saveNewOrder(Ordertxn order_txn) {
		try {
			saveOrUpdate(order_txn);
			return order_txn.getId();
		} catch (Exception e) {
			throw new UnexpectedDBException("Error in adding order.", e);
		}
	}
	
	/**
	 * @param couponCode
	 * @return
	 */
	public OfferCoupon fetchOfferByCouponCode(String couponCode)
	{
		try {
			String hqlQuery = "from OfferCoupon c where c.couponCode = '"+couponCode+"'";
			List<OfferCoupon> coupons  = executeHQL(hqlQuery);
			if(coupons.size() > 0)
			{
				return coupons.get(0);
			}
			return null;
		} catch (Exception e) {
			throw new UnexpectedDBException("Not able to find coupon with code "+couponCode, e);
		}
	}
	
	/**
	 * @return
	 */
	public List<Payment_Type> fetchAllPaymentTypes() {
		try {
			return selectAll("Payment_Type");
		} catch (Exception e) {
			throw new UnexpectedDBException("No payment type is available", e);
		}
	}
	
	/**
	 * @return
	 */
	public List<BankDetails> fetchAllBanks() {
		try {
			return selectAll("BankDetails");
		} catch (Exception e) {
			throw new UnexpectedDBException("No BANK is available", e);
		}
	}

}
